import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {Location} from "@angular/common";
import {NzTableQueryParams} from "ng-zorro-antd/table";
import {SearchCommonVO, temperatureManage, UserLogin} from "../../../core/services/types";
import {finalize, takeUntil} from "rxjs/operators";
import {UserInfoKey} from "../../../configs/constant";
import {UserTemperatureService} from "../../../core/services/http/user-temperature.service";
import {NzModalService} from "ng-zorro-antd/modal";
import {MessageService} from "../../../core/services/common/message.service";
import {UserInfoService} from "../../../core/services/store/user-info.service";
import {WindowService} from "../../../core/services/common/window.service";
import {MyTableConfig} from "../../../share/components/ant-table/ant-table.component";
import {Subject} from "rxjs";
import {ActivatedRoute} from "@angular/router";

@Component({
  selector: 'app-personal-temperature',
  templateUrl: './personal-temperature.component.html',
  styleUrls: ['./personal-temperature.component.less']
})
export class PersonalTemperatureComponent implements OnInit {
  userLogin!: UserLogin; // 当前选中的任务对象
  tableConfig!: MyTableConfig;
  dataList: temperatureManage[] = [];
  checkedCashArray: temperatureManage[] = [];
  personId!: number;
  private destory$ = new Subject<void>();

  constructor(private dataService: UserTemperatureService, private modalSrv: NzModalService, private cdr: ChangeDetectorRef,
              private messageService: MessageService,
              private location: Location,
              private userInfoService: UserInfoService,
              private windowServe: WindowService, private activatedRoute: ActivatedRoute) {
  }

  back(): void {
    this.location.back();
  }

  selectedChecked(e: any): void {
    this.checkedCashArray = [...e];
  }


  getDataList(e?: NzTableQueryParams): void {
    this.tableConfig.loading = true;
    const params: SearchCommonVO<any> = {
      pageSize: this.tableConfig.pageSize!,
      pageNum: e?.pageIndex || this.tableConfig.pageIndex!,
      filters: {personId: this.personId}
    };
    this.dataService.getUserTemperatureList(params).pipe(finalize(() => {
      this.tableLoading(false);
    })).subscribe((data => {
      const {list, total, pageNum} = data;
      this.dataList = [...list];
      this.tableConfig.total = total!;
      this.tableConfig.pageIndex = pageNum!;
      this.tableLoading(false);
      this.checkedCashArray = [...this.checkedCashArray];
    }));
  }


  // 触发表格变更检测
  tableChangeDectction(): void {
    // 改变引用触发变更检测。
    this.dataList = [...this.dataList];
    this.cdr.detectChanges();
  }

  tableLoading(isLoading: boolean): void {
    this.tableConfig.loading = isLoading;
    this.tableChangeDectction();
  }


  // 修改一页几条

  changePageSize(e: number): void {
    this.tableConfig.pageSize = e;
  }

  initParam(): void {
    this.userInfoService.getLoginUserInfoMode().pipe(takeUntil(this.destory$)).subscribe(currentUserLogin => {
      this.userLogin = currentUserLogin;
      if (this.userLogin.userId === -1) {
        this.userLogin = JSON.parse(this.windowServe.getStorage(UserInfoKey)!);
        this.userInfoService.setLoginUserInfoMode(this.userLogin);
      }
    });
  }

  ngOnDestroy(): void {
    this.destory$.next();
    this.destory$.complete();
  }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParam => {
      this.personId = queryParam.id;
    });
    console.log(this.personId);
    this.initParam();
    this.initTable();
  }

  private initTable(): void {
    this.tableConfig = {
      showCheckbox: false,
      headers: [
        {
          title: '姓名',
          field: 'name',
          width: 100,
        },
        {
          title: '身份证号',
          field: 'personId',
          width: 110,
        },
        {
          title: '温度',
          field: 'temperature',
          width: 60,
        },
        {
          title: '部门',
          field: 'department',
          width: 100,
        },
        {
          title: '地址',
          field: 'address',
          width: 150,
        },
        {
          title: '打卡时间',
          field: 'createTime',
          pipe: 'date:yyyy-MM-dd/hh:mm:ss',
          width: 100,
        },
      ],
      total: 0,
      loading: true,
      pageSize: 10,
      pageIndex: 1,
    };
  }
}
